package data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class TimeStock implements Serializable {

	private static final long serialVersionUID = -7657041266153838204L;
	private TimeStock lastStock;
	private String code;
	private String stockName;
	private BigDecimal beginPrice;
	private BigDecimal preDayPrice;
	private BigDecimal curPrice;
	private BigDecimal maxPrice;
	private BigDecimal minPrice;
	private BigDecimal curBuyPrice;
	private BigDecimal curSalePrice;
	private int qty;
	private BigDecimal amount;
	private int firstBuyqty;
	private BigDecimal firstBuyPrice;
	private int secondBuyqty;
	private BigDecimal secondBuyPrice;
	private int thirdBuyqty;
	private BigDecimal thirdBuyPrice;
	private int forthBuyqty;
	private BigDecimal forthBuyPrice;
	private int fifthBuyqty;
	private BigDecimal fifthBuyPrice;
	private int firstSaleqty;
	private BigDecimal firstSalePrice;
	private int secondSaleqty;
	private BigDecimal secondSalePrice;
	private int thirdSaleqty;
	private BigDecimal thirdSalePrice;
	private int forthSaleqty;
	private BigDecimal forthSalePrice;
	private int fifthSaleqty;
	private BigDecimal fifthSalePrice;
	private Date date;
	private Time time;
	private String industry;
	private String sinaIndustry;
	
	private BigDecimal priceWive = BigDecimal.ZERO;
	private BigDecimal selectPrice  ;
	private Time selectTime;
	private int highHit;
	private Time amountInrecTime;
	private int qtyBat;
	private boolean isHead;
	
	private String mark;
	
	private BigDecimal aver30 = BigDecimal.ZERO;
	private BigDecimal aver20 = BigDecimal.ZERO;
	private BigDecimal aver10 = BigDecimal.ZERO;
	
	private int info;
	
	private BigDecimal ratePercent = BigDecimal.ZERO;
	
	public Time getAmountInrecTime() {
		return amountInrecTime;
	}
	
	public void setAmountInrecTime(Time amountInrecTime) {
		this.amountInrecTime = amountInrecTime;
	}
	
	public int getQtyBat() {
		return qtyBat;
	}
	
	public void setQtyBat(int qtyBat) {
		this.qtyBat = qtyBat;
	}
	
	public BigDecimal getPriceWive() {
		return priceWive;
	}
	
	public void setPriceWive(BigDecimal priceWive) {
		this.priceWive = priceWive;
	}
	
	public BigDecimal getSelectPrice() {
		return selectPrice;
	}
	
	public void setSelectPrice(BigDecimal selectPrice) {
		this.selectPrice = selectPrice;
	}
	
	public Time getSelectTime() {
		return selectTime;
	}
	
	public void setSelectTime(Time selectTime) {
//		if(selectTime.getHours()>14){
//			selectTime = null;
//		}
		this.selectTime = selectTime;
	}
	
	public BigDecimal getRatePercent() {
		return ratePercent;
	}

	public void setRatePercent(BigDecimal ratePercent) {
		this.ratePercent = ratePercent;
	}

	public String getIndustry() {
		return industry;
	}

	public void setIndustry(String industry) {
		this.industry = industry;
	}

	public TimeStock getLastStock() {
		return lastStock;
	}

	public void setLastStock(TimeStock lastStock) {
		this.lastStock = lastStock;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getStockName() {
		return stockName;
	}

	public void setStockName(String name) {
		this.stockName = name;
	}

	public BigDecimal getBeginPrice() {
		return beginPrice;
	}

	public void setBeginPrice(BigDecimal beginPrice) {
		this.beginPrice = beginPrice;
	}

	public BigDecimal getPreDayPrice() {
		return preDayPrice;
	}

	public void setPreDayPrice(BigDecimal preDayPrice) {
		this.preDayPrice = preDayPrice;
	}

	public BigDecimal getCurPrice() {
		return curPrice;
	}

	public void setCurPrice(BigDecimal curPrice) {
		this.curPrice = curPrice;
	}

	public BigDecimal getMaxPrice() {
		return maxPrice;
	}

	public void setMaxPrice(BigDecimal maxPrice) {
		this.maxPrice = maxPrice;
	}

	public BigDecimal getMinPrice() {
		return minPrice;
	}

	public void setMinPrice(BigDecimal minPrice) {
		this.minPrice = minPrice;
	}

	public BigDecimal getCurBuyPrice() {
		return curBuyPrice;
	}

	public void setCurBuyPrice(BigDecimal curBuyPrice) {
		this.curBuyPrice = curBuyPrice;
	}

	public BigDecimal getCurSalePrice() {
		return curSalePrice;
	}

	public void setCurSalePrice(BigDecimal curSalePrice) {
		this.curSalePrice = curSalePrice;
	}

	public int getQty() {
		return qty;
	}

	public void setQty(int qty) {
		this.qty = qty;
	}

	public BigDecimal getAmount() {
		return amount.divide(new BigDecimal(10000),2,BigDecimal.ROUND_HALF_UP);
	}

	public void setAmount(BigDecimal amount) {
		this.amount = amount;
	}

	public int getFirstBuyqty() {
		return firstBuyqty;
	}

	public void setFirstBuyqty(int firstBuyqty) {
		this.firstBuyqty = firstBuyqty;
	}

	public BigDecimal getFirstBuyPrice() {
		return firstBuyPrice;
	}

	public void setFirstBuyPrice(BigDecimal firstBuyPrice) {
		this.firstBuyPrice = firstBuyPrice;
	}

	public int getSecondBuyqty() {
		return secondBuyqty;
	}

	public void setSecondBuyqty(int secondBuyqty) {
		this.secondBuyqty = secondBuyqty;
	}

	public BigDecimal getSecondBuyPrice() {
		return secondBuyPrice;
	}

	public void setSecondBuyPrice(BigDecimal secondBuyPrice) {
		this.secondBuyPrice = secondBuyPrice;
	}

	public int getThirdBuyqty() {
		return thirdBuyqty;
	}

	public void setThirdBuyqty(int thirdBuyqty) {
		this.thirdBuyqty = thirdBuyqty;
	}

	public BigDecimal getThirdBuyPrice() {
		return thirdBuyPrice;
	}

	public void setThirdBuyPrice(BigDecimal thirdBuyPrice) {
		this.thirdBuyPrice = thirdBuyPrice;
	}

	public int getForthBuyqty() {
		return forthBuyqty;
	}

	public void setForthBuyqty(int forthBuyqty) {
		this.forthBuyqty = forthBuyqty;
	}

	public BigDecimal getForthBuyPrice() {
		return forthBuyPrice;
	}

	public void setForthBuyPrice(BigDecimal forthBuyPrice) {
		this.forthBuyPrice = forthBuyPrice;
	}

	public int getFifthBuyqty() {
		return fifthBuyqty;
	}

	public void setFifthBuyqty(int fifthBuyqty) {
		this.fifthBuyqty = fifthBuyqty;
	}

	public BigDecimal getFifthBuyPrice() {
		return fifthBuyPrice;
	}

	public void setFifthBuyPrice(BigDecimal fifthBuyPrice) {
		this.fifthBuyPrice = fifthBuyPrice;
	}

	public int getFirstSaleqty() {
		return firstSaleqty;
	}

	public void setFirstSaleqty(int firstSaleqty) {
		this.firstSaleqty = firstSaleqty;
	}

	public BigDecimal getFirstSalePrice() {
		return firstSalePrice;
	}

	public void setFirstSalePrice(BigDecimal firstSalePrice) {
		this.firstSalePrice = firstSalePrice;
	}

	public int getSecondSaleqty() {
		return secondSaleqty;
	}

	public void setSecondSaleqty(int secondSaleqty) {
		this.secondSaleqty = secondSaleqty;
	}

	public BigDecimal getSecondSalePrice() {
		return secondSalePrice;
	}

	public void setSecondSalePrice(BigDecimal secondSalePrice) {
		this.secondSalePrice = secondSalePrice;
	}

	public int getThirdSaleqty() {
		return thirdSaleqty;
	}

	public void setThirdSaleqty(int thirdSaleqty) {
		this.thirdSaleqty = thirdSaleqty;
	}

	public BigDecimal getThirdSalePrice() {
		return thirdSalePrice;
	}

	public void setThirdSalePrice(BigDecimal thirdSalePrice) {
		this.thirdSalePrice = thirdSalePrice;
	}

	public int getForthSaleqty() {
		return forthSaleqty;
	}

	public void setForthSaleqty(int forthSaleqty) {
		this.forthSaleqty = forthSaleqty;
	}

	public BigDecimal getForthSalePrice() {
		return forthSalePrice;
	}

	public void setForthSalePrice(BigDecimal forthSalePrice) {
		this.forthSalePrice = forthSalePrice;
	}

	public int getFifthSaleqty() {
		return fifthSaleqty;
	}

	public void setFifthSaleqty(int fifthSaleqty) {
		this.fifthSaleqty = fifthSaleqty;
	}

	public BigDecimal getFifthSalePrice() {
		return fifthSalePrice;
	}

	public void setFifthSalePrice(BigDecimal fifthSalePrice) {
		this.fifthSalePrice = fifthSalePrice;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public Time getTime() {
		return time;
	}

	public void setTime(Time time) {
		this.time = time;
	}

	public void refresh(){

			String string = StockURLDataBuffer.getUrlData(code);
			
			if (string == null) {
				return;
			}

			String[] s = string.split(",");
//			System.out.println(string);
			if (s.length < 32 && !code.equals("sz399001")) {
				System.out.println("废弃" + code + ":" + string);
				return;
			}else if(s.length < 32 && code.equals("sz399001")){
				return;
			}
			try{
				synchronized (this) {
					String[] time = s[31].split(":");
					Time t = new Time(Integer.parseInt(time[0]),
							Integer.parseInt(time[1]),
							Integer.parseInt(time[2]));
					if(t.equals(this.time)){
						return;
					}
					this.setStockName(s[0]);
					this.setBeginPrice(new BigDecimal(s[1]).setScale(2,
							BigDecimal.ROUND_HALF_UP));
					this.setPreDayPrice(new BigDecimal(s[2]).setScale(2,
							BigDecimal.ROUND_HALF_UP));
					this.setCurPrice(new BigDecimal(s[3]).setScale(2,
							BigDecimal.ROUND_HALF_UP));
					BigDecimal maxPriceTemp = new BigDecimal(s[4]).setScale(2,BigDecimal.ROUND_HALF_UP);
					if(maxPrice!=null&&maxPriceTemp.compareTo(maxPrice)>0){
						highHit++;
					}else{
						highHit=0;
					}
					
					this.setMaxPrice(maxPriceTemp);
					this.setMinPrice(new BigDecimal(s[5]).setScale(2,
							BigDecimal.ROUND_HALF_UP));
					this.setCurBuyPrice(new BigDecimal(s[6]).setScale(2,
							BigDecimal.ROUND_HALF_UP));
					this.setCurSalePrice(new BigDecimal(s[7]).setScale(2,
							BigDecimal.ROUND_HALF_UP));
					try {
						int curQty = (int) (Integer.parseInt(s[8]));
						this.setQty(curQty);
					} catch (NumberFormatException e) {
						this.setQty(0);
					}
					this.setAmount(new BigDecimal(s[9]).setScale(2));
					if (s.length > 31) {
						if(s[11].equals(s[21])&&(!s[11].equals("0")&&!code.equals("sz399001")&&code.equals("sh000001"))){
							this.setCurPrice(new BigDecimal(s[11]));
						}
						this.setFirstBuyqty(Integer.parseInt(s[10]));
						this.setFirstBuyPrice(new BigDecimal(s[11]).setScale(2));
						this.setSecondBuyqty(Integer.parseInt(s[12]));
						this.setSecondBuyPrice(new BigDecimal(s[13]).setScale(2));
						this.setThirdBuyqty(Integer.parseInt(s[14]));
						this.setThirdBuyPrice(new BigDecimal(s[15]).setScale(2));
						this.setForthBuyqty(Integer.parseInt(s[16]));
						this.setForthBuyPrice(new BigDecimal(s[17]).setScale(2));
						this.setFifthBuyqty(Integer.parseInt(s[18]));
						this.setFifthBuyPrice(new BigDecimal(s[19]).setScale(2));
						this.setFirstSaleqty(Integer.parseInt(s[20]));
						this.setFirstSalePrice(new BigDecimal(s[21]).setScale(2));
						this.setSecondSaleqty(Integer.parseInt(s[22]));
						this.setSecondSalePrice(new BigDecimal(s[23]).setScale(2));
						this.setThirdSaleqty(Integer.parseInt(s[24]));
						this.setThirdSalePrice(new BigDecimal(s[25]).setScale(2));
						this.setForthSaleqty(Integer.parseInt(s[26]));
						this.setForthSalePrice(new BigDecimal(s[27]).setScale(2));
						this.setFifthSaleqty(Integer.parseInt(s[28]));
						this.setFifthSalePrice(new BigDecimal(s[29]).setScale(2));
						SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
						try {
							this.setDate(new Date(sdf.parse(s[30]).getTime()));
						} catch (ParseException e) {
							e.printStackTrace();
						}
						this.setTime(t);
						
						if(curBuyPrice.compareTo(curSalePrice)==0&&!"sz399001".equals(code)&&!"sh000001".equals(code)){
							curPrice = curBuyPrice;
						}
						ratePercent = curPrice.subtract(preDayPrice).multiply(Constant.ONEHUNDRED).divide(preDayPrice,2,BigDecimal.ROUND_HALF_UP);
						
					}
				}
			}catch(Exception e){
				e.printStackTrace();
			}
	
	}

	public int getInfo() {
		return info;
	}

	public void setInfo(int info) {
		this.info = info;
	}

	public String getSinaIndustry() {
		return sinaIndustry;
	}

	public void setSinaIndustry(String sinaIndustry) {
		this.sinaIndustry = sinaIndustry;
	}

	public BigDecimal getAver30() {
		return aver30;
	}

	public void setAver30(BigDecimal aver30) {
		this.aver30 = aver30;
	}

	public BigDecimal getAmountBat() {
		if (getLastStock() == null
				|| getLastStock().getAmount()
						.compareTo(Constant.ZERO) == 0) {
			return BigDecimal.ZERO;
		}
		return getAmount().divide(getLastStock().getAmount(), 2,BigDecimal.ROUND_HALF_UP);
	}
	
	public void selected(){
		if(selectTime==null){
			setSelectTime(time);
			setSelectPrice(curPrice);
		}
	}

	public boolean isHead() {
		return isHead;
	}

	public void setHead(boolean isHead) {
		this.isHead = isHead;
	}

	public BigDecimal getDayHealth() {
		if(getAmountBat().compareTo(BigDecimal.ZERO)==0){
			return BigDecimal.ZERO;
		}else{
			return getRatePercent().divide(getAmountBat(), 2,BigDecimal.ROUND_HALF_UP);
		}
	}

	public String getMark() {
		return mark;
	}

	public void setMark(String mark) {
		this.mark = mark;
	}

	public BigDecimal getAver20() {
		return aver20;
	}

	public void setAver20(BigDecimal aver20) {
		this.aver20 = aver20;
	}

	public int getHighHit() {
		return highHit;
	}

	public void setHighHit(int highHit) {
		this.highHit = highHit;
	}

	public BigDecimal getAver10() {
		return aver10;
	}

	public void setAver10(BigDecimal aver10) {
		this.aver10 = aver10;
	}

}
